<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>3. Tutorial</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../../../index.html" title="ODTONE 0.3">
<link rel="up" href="../index_sap.html" title="ODTONE SAP">
<link rel="prev" href="odtone_sap_link_class.html" title="2.3. odtone::sap::link Class Reference">
<link rel="next" href="../../app.html" title="Applications">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="'ODTONE - Open Dot Twenty One'" width="100" height="100" src="../../.././images/logo.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="odtone_sap_link_class.html"><img src="../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index_sap.html"><img src="../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="../../app.html"><img src="../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" title="3. Tutorial">
<div class="titlepage"><div><div><h4 class="title">
<a name="odtone.library.index_sap.sap_tutorial"></a><a class="link" href="sap_tutorial.html" title="3. Tutorial"> 3. Tutorial</a>
</h4></div></div></div>
<p>
          The tutorial programs in this section shows how to use ODTONE SAP to develop
          simple SAPs.
        </p>
<p>
          <span class="bold"><strong>Receiving MIH Messages</strong></span>
        </p>
<p>
          In this example we have very basic running MIH User SAP that only receives
          MIH messages (MIH Link SAP implementation is similar):
        </p>
<p>
          To receive messages, we need a callback handler:
        </p>
<p>
          
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">event_handler</span><span class="special">(</span><span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">message</span><span class="special">&amp;</span> <span class="identifier">pm</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
<span class="special">{</span>
	<span class="keyword">if</span> <span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span>
		<span class="comment">//do something with the error
</span>		<span class="keyword">return</span><span class="special">;</span>
	<span class="special">}</span>

	<span class="keyword">switch</span> <span class="special">(</span><span class="identifier">pm</span><span class="special">.</span><span class="identifier">mid</span><span class="special">())</span> <span class="special">{</span>
		<span class="keyword">case</span> <span class="identifier">odtone</span><span class="special">::</span><span class="identifier">indication</span><span class="special">::</span><span class="identifier">link_up</span><span class="special">:</span>
			<span class="comment">//...
</span>			<span class="keyword">break</span><span class="special">;</span>

		<span class="keyword">default</span><span class="special">:</span>
			<span class="comment">//...
</span>	<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
        </p>
<p>
          And a simple main:
        </p>
<p>
          
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
	<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="identifier">ios</span><span class="special">;</span>
	<span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">config</span> <span class="identifier">cfg</span><span class="special">;</span>
	<span class="identifier">odtone</span><span class="special">::</span><span class="identifier">sap</span><span class="special">::</span><span class="identifier">user</span> <span class="identifier">mih_usr</span><span class="special">(</span><span class="identifier">cfg</span><span class="special">,</span> <span class="identifier">ios</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">event_handler</span><span class="special">);</span>

	<span class="identifier">ios</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
        </p>
<p>
          <span class="bold"><strong>Sending MIH Messages</strong></span>
        </p>
<p>
          In this second example we added the ability to send MIH messages (MIH Link
          SAP implementation is similar):
        </p>
<p>
          To send messages, we need a callback handler:
        </p>
<p>
          
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">register_response_handler</span><span class="special">(</span><span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">message</span><span class="special">&amp;</span> <span class="identifier">msg</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
<span class="special">{</span>
	<span class="keyword">if</span> <span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span>
		<span class="comment">//do something with the error
</span>		<span class="keyword">return</span><span class="special">;</span>
	<span class="special">}</span>

	<span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">status</span> <span class="identifier">st</span><span class="special">;</span>

	<span class="identifier">msg</span> <span class="special">&gt;&gt;</span> <span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">response</span><span class="special">()</span>
		 <span class="special">&amp;</span> <span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">tlv_status</span><span class="special">(</span><span class="identifier">st</span><span class="special">);</span>

	<span class="comment">//...
</span><span class="special">}</span>
</pre>
<p>
        </p>
<p>
          And a few modifications in the main to send the message:
        </p>
<p>
          
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
	<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="identifier">ios</span><span class="special">;</span>
	<span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">config</span> <span class="identifier">cfg</span><span class="special">;</span>
	<span class="identifier">odtone</span><span class="special">::</span><span class="identifier">sap</span><span class="special">::</span><span class="identifier">user</span> <span class="identifier">usr</span><span class="special">(</span><span class="identifier">cfg</span><span class="special">,</span> <span class="identifier">ios</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">event_handler</span><span class="special">);</span>

	<span class="identifier">msg</span> <span class="special">&lt;&lt;</span> <span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">request</span><span class="special">(</span><span class="identifier">odtone</span><span class="special">::</span><span class="identifier">mih</span><span class="special">::</span><span class="identifier">request</span><span class="special">::</span><span class="identifier">mih_register</span><span class="special">);</span>

	<span class="identifier">usr</span><span class="special">.</span><span class="identifier">async_send</span><span class="special">(</span><span class="identifier">msg</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">register_response_handler</span><span class="special">);</span>

	<span class="identifier">ios</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009 -2011 Universidade Aveiro<br>Copyright &#169; 2009 -2011 Instituto de Telecomunica&#231;&#245;es - P&#243;lo Aveiro<p>
        This software is distributed under a license. The full license agreement
        can be found in the LICENSE in this distribution. This software may not be
        copied, modified, sold or distributed other than expressed in the named license
        agreement. This software is distributed without any warranty.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="odtone_sap_link_class.html"><img src="../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index_sap.html"><img src="../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="../../app.html"><img src="../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>
